package cn.xiaopengstack.domain.agent.service.execute.auto.factory;

import cn.bugstack.wrench.design.framework.tree.StrategyHandler;
import cn.xiaopengstack.domain.agent.model.entity.ExecuteCommandEntity;
import cn.xiaopengstack.domain.agent.model.valobj.AiAgentClientFlowConfigVO;
import cn.xiaopengstack.domain.agent.service.execute.auto.node.RootNode;
import jakarta.annotation.Resource;
import lombok.*;
import org.checkerframework.checker.units.qual.K;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jiangyangang
 */
@Service
public class DefaultAutoAgentExecuteStrategyFactory {

    @Resource(name = "executeRootNode")
    private RootNode rootNode;

    public StrategyHandler<ExecuteCommandEntity, DynamicContext, String> getAgentStrategyHandler(){
        return rootNode;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public static class DynamicContext{

        public enum Key{
            /**
             * AI对任务当前任务分析的结果
             */
            analysisResult,
            /**
             * AI对当前分析结果的执行总结结果
             */
            executionResult,
            /**
             * AI对当前执行总结的质量分析结果
             */
            supervisionResult,
            /**
             * AI对当前分析结果的总结
             */
            finalSummary,
            ;
        }

        /**
         * 任务执行步骤
         */
        private int step = 1;

        /**
         * 最大任务步骤
         */
        private int maxSteps = 1;

        /**
         * 执行历史
         */
        private StringBuilder executionHistory;

        /**
         * 当前任务
         */
        private String currentTask;

        /**
         * 任务是否完成
         */
        boolean isCompleted = false;

        /**
         * client的配置
         */
        private Map<String, AiAgentClientFlowConfigVO> aiAgentClientFlowConfigVOMap;

        /**
         * 字典
         */
        private Map<Key, Object> dataObjects = new HashMap<>();

        /**
         * 流式响应体
         */
        private ResponseBodyEmitter emitter;

        public <T> void setValue(Key key, T value) {
            dataObjects.put(key, value);
        }

        public <T> T getValue(Key key) {
            return (T) dataObjects.get(key);
        }

    }
}
